//
//  PKRadioTableViewHeaderView.m
//  PianKe
//
//  Created by lanou on 15/10/4.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//

#import "PKRadioTableViewHeaderView.h"
#import "PKRadioMainListModel.h"
#import <UIImageView+WebCache.h>
@interface PKRadioTableViewHeaderView()<UIScrollViewDelegate>
@end

@implementation PKRadioTableViewHeaderView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WITDTH, SCREEN_HEIGHT/5)];
        _mainScrollView.contentSize = CGSizeMake(SCREEN_WITDTH * 3, SCREEN_HEIGHT/5);
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.delegate = self;
        
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WITDTH-50, SCREEN_HEIGHT/5-20, 50, 20)];
        _pageControl.numberOfPages = 3;
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        
        _imageView1 = [[UIImageView alloc]init];
        _imageView1.frame = CGRectMake(5, SCREEN_HEIGHT/5 + 5, (SCREEN_WITDTH-20)/3, (SCREEN_WITDTH-20)/3);
        
        _imageView2 = [[UIImageView alloc]init];
        _imageView2.frame = CGRectMake((SCREEN_WITDTH-20)/3 + 10, SCREEN_HEIGHT/5 + 5, (SCREEN_WITDTH-20)/3, (SCREEN_WITDTH-20)/3);
        
        _imageView3 = [[UIImageView alloc]init];
        _imageView3.frame = CGRectMake((SCREEN_WITDTH-20)*2/3 + 15, SCREEN_HEIGHT/5 + 5, (SCREEN_WITDTH-20)/3, (SCREEN_WITDTH-20)/3);
        
        _allStationLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 265, SCREEN_WITDTH*2/5, 20)];
        _allStationLabel.text = @"全部电台.All stations";
        _allStationLabel.font = [UIFont systemFontOfSize:15];
        
        _singleLine = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WITDTH*2/5, 275, SCREEN_WITDTH*3/5, 1)];
        _singleLine.backgroundColor = [UIColor colorWithWhite:0.829 alpha:1.000];
        
        [self addSubview:_mainScrollView];
        [self addSubview:_pageControl];
        [self addSubview:_imageView1];
        [self addSubview:_imageView2];
        [self addSubview:_imageView3];
        [self addSubview:_allStationLabel];
        [self addSubview:_singleLine];
        
        
      }
        return self;
}

-(void)configureData:(NSMutableArray *)imageArray
{
    for (int i = 0; i<imageArray.count ; i++) {
        if (i < 3) {
            UIImageView *scrollImageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*SCREEN_WITDTH, 0, SCREEN_WITDTH, SCREEN_HEIGHT/5)];
            [scrollImageView sd_setImageWithURL:[NSURL URLWithString:imageArray[i]]];
            [_mainScrollView addSubview:scrollImageView];
        }else
        {
            switch (i) {
                case 3:
                    [_imageView1 sd_setImageWithURL:[NSURL URLWithString:imageArray[3]]];
                    break;
                    case 4:
                    [_imageView2 sd_setImageWithURL:[NSURL URLWithString:imageArray[4]]];
                    break;
                    case 5:
                    [_imageView3 sd_setImageWithURL:[NSURL URLWithString:imageArray[5]]];
                    break;
                default:
                    break;
            }
        }
        
    }
    
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(imageScroll) userInfo:nil repeats:YES];
    
}

static NSInteger i = 0;
-(void)imageScroll
{
    _mainScrollView.contentOffset = CGPointMake(SCREEN_WITDTH * i, 0);
    _pageControl.currentPage = i;
    i++;
    if (i >2) {
        i = 0;
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    _pageControl.currentPage = scrollView.contentOffset.x/SCREEN_WITDTH;
}





@end
